unit uFindTranceEx;

interface

uses
  WinApi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm, uniGUIBaseClasses, uniBasicGrid, uniDBGrid,
  uniPanel, uniPageControl, Data.DB, Datasnap.DBClient, ClientRowSet,
  Clipbrd;

type
  TFrmFindTranceEx = class(TUniForm)
    pgTrance: TUniPageControl;
    TabDetail: TUniTabSheet;
    TabTrance: TUniTabSheet;
    GridBill: TUniDBGrid;
    gridTrance: TUniDBGrid;
    crsBill: TClientRowSet;
    dsBill: TDataSource;
    dsTrance: TDataSource;
    UniDBGrid1: TUniDBGrid;
    crsTrance: TClientRowSet;
    crsTranceID: TStringField;
    crsTranceBILL_ID: TStringField;
    crsTranceBILL_NO: TStringField;
    crsTranceDIRECTION: TIntegerField;
    crsTranceTRACK_NO: TStringField;
    crsTrancePKG_NO: TStringField;
    crsTranceOP_CODE: TStringField;
    crsTranceOPERTION: TStringField;
    crsTranceOP_TIME: TStringField;
    crsTranceREC_TIME: TStringField;
    crsTranceGROUP_CODE: TStringField;
    crsTranceNODE_NO: TStringField;
    crsTranceNODE_NAME: TStringField;
    crsTranceNODE_TEL: TStringField;
    crsTranceEMP_NO: TStringField;
    crsTranceEMP_NAME: TStringField;
    crsTranceBATCH_NO: TStringField;
    crsTranceUSER_NO: TStringField;
    crsTranceREMARK: TStringField;
    crsTranceSYNC_FLAG: TStringField;
    crsTranceSYS_ACCOUNT: TStringField;
    crsTranceVERSION: TIntegerField;
    crsBillSUB_BILL_NO: TStringField;
    crsTranceNEXT_NODE: TStringField;
    crsTranceNEXT_NAME: TStringField;
    crsTranceUSER_NAME: TStringField;
    crsOper: TClientRowSet;
    procedure GridBillCellClick(Column: TUniDBGridColumn);
    procedure GridBillColumnSort(Column: TUniDBGridColumn; Direction: Boolean);
    procedure gridTranceColumnSort(Column: TUniDBGridColumn;
      Direction: Boolean);
    procedure UniFormShow(Sender: TObject);
    procedure GridBillDblClick(Sender: TObject);
    procedure gridTranceCellClick(Column: TUniDBGridColumn);
    procedure gridTranceDblClick(Sender: TObject);
    procedure UniFormCreate(Sender: TObject);
    procedure crsTranceOPERTIONGetText(Sender: TField; var Text: string;
      DisplayText: Boolean);
  private
    { Private declarations }
    currField: TField;
    procedure FindTrance(billNo: string);
  public
    { Public declarations }
    procedure ShowTranceEx(billNo: string);
  end;

function FrmFindTranceEx: TFrmFindTranceEx;

implementation

{$R *.dfm}

uses
  MainModule, uniGUIApplication, ExpModule, uCommFunction, DictModule;

function FrmFindTranceEx: TFrmFindTranceEx;
begin
  Result := TFrmFindTranceEx(UniMainModule.GetFormInstance(TFrmFindTranceEx));

end;

{ TFrmFindTranceEx }

procedure TFrmFindTranceEx.crsTranceOPERTIONGetText(Sender: TField;
  var Text: string; DisplayText: Boolean);
begin
  Text := Sender.AsString;
  if dmExpress.UserInfo.LanguageType = 'ENG' then
  begin
    if crsOper.Locate('paramcode', crsTranceop_code.AsInteger, []) then
      Text := crsOper.FieldByName('captioneng').AsString
    else
      Text := Sender.AsString;
  end;
end;

procedure TFrmFindTranceEx.FindTrance(billNo: string);
begin
  crsTrance.Close;
  {crsTrance.CommandText := 'select ' + GetClientRowSetColumns(crsTrance) + ' from TSP_TRANCE where GROUP_CODE=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
    ' and BILL_NO=' + QuotedStr(billNo) + ' order by REC_TIME desc'; }
  crsTrance.CommandText := 'SELECT t.*, u.user_name FROM tsp_trance t ' +
    ' LEFT JOIN sys_user u ON u.user_no=t.user_no AND u.group_code=t.group_code ' +
    ' WHERE t.group_code=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
    ' AND t.bill_no=' + QuotedStr(billNo) + ' ORDER BY t.rec_time DESC';
  crsTrance.Open;
end;

procedure TFrmFindTranceEx.GridBillCellClick(Column: TUniDBGridColumn);
begin
  currField := Column.Field;
  FindTrance(crsBill.FieldByName('sub_bill_no').AsString);
end;

procedure TFrmFindTranceEx.GridBillColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  SortColumn((GridBill.DataSource.DataSet as TClientRowSet), Column.FieldName, Direction);
end;

procedure TFrmFindTranceEx.GridBillDblClick(Sender: TObject);
begin
  if currField <> nil then
    Clipboard.AsText := currField.AsString;
end;

procedure TFrmFindTranceEx.gridTranceCellClick(Column: TUniDBGridColumn);
begin
  currField := Column.Field;
end;

procedure TFrmFindTranceEx.gridTranceColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  SortColumn((gridTrance.DataSource.DataSet as TClientRowSet), Column.FieldName, Direction);
end;

procedure TFrmFindTranceEx.gridTranceDblClick(Sender: TObject);
begin
  if currField <> nil then
    Clipboard.AsText := currField.AsString;
end;

procedure TFrmFindTranceEx.ShowTranceEx(billNo: string);
var
  I: Integer;
begin
  for I := 0 to pgTrance.PageCount - 1 do
    pgTrance.Pages[I].TabVisible := False;

  crsBill.Close;
  crsBill.CommandText := 'select SUB_BILL_NO from TSP_ORDER_DETAIL where TSP_GROUP=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
    ' and BILL_NO='  + QuotedStr(billNo);
  crsBill.Open;
  if crsBill.RecordCount > 0 then
  begin
    if crsBill.RecordCount > 1 then
    begin
      GridBillCellClick(GridBill.Columns[0]);
      pgTrance.ActivePage := TabDetail;
    end
    else
    begin
      FindTrance(billNo);
      pgTrance.ActivePage := TabTrance;
    end;
  end
  else
  begin
    FindTrance(billNo);
    pgTrance.ActivePage := TabTrance;
  end;
  FrmFindTranceEx.ShowModal;
end;

procedure TFrmFindTranceEx.UniFormCreate(Sender: TObject);
begin
  SetCompoentLanguage(Self);
  crsOper.Data := dmDict.crsLookup.Data;
  crsOper.Filter := 'PARAMTYPE=' + quotedstr('TSPORDER_STATE');
  crsOper.Filtered := True;
end;

procedure TFrmFindTranceEx.UniFormShow(Sender: TObject);
begin
  SetFieldsSort(GridBill);
  SetFieldsSort(gridTrance);
  currField := nil;
end;

end.
